package persistencia;

import java.util.Vector;
import negocio.Imovel;
import negocio.Proprietario;
import negocio.ImobiliariaException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


	public class ListaImovelDAO extends BaseDAO {

		        private PreparedStatement stIncluir;
		        private PreparedStatement stConsultaEndereco;
		        private PreparedStatement stConsultaRegImov;
		        private PreparedStatement stConsultaProp;
		        private PreparedStatement stListaValor;
		        private PreparedStatement stAlterar;

		        public ListaImovelDAO() throws ImobiliariaException {
		            super();
		            Connection conect = getConexao();
		            try {
		                stIncluir = conect.prepareStatement("INSERT INTO imovel (regimovel, cpfprop, endereco, valor) VALUES (?, ?, ?, ? )");
		                stConsultaEndereco = conect.prepareStatement("SELECT * FROM imovel WHERE endereco LIKE %?%");
		                stConsultaRegImov = conect.prepareStatement("SELECT * FROM imovel WHERE regimovel = ?");
		                stConsultaProp = conect.prepareStatement("SELECT * FROM imovel WHERE cpfprop = ?");
		                stListaValor = conect.prepareStatement("SELECT * FROM imovel ORDER BY valor ");
		                stAlterar = conect.prepareStatement("UPDATE imovel SET regimovel=?, cpfprop=?, endereco=?, valor = ?");
		            } catch(SQLException e) {
		                throw new ImobiliariaException("Nao foi possivel criar as consultas dos proprietario: " + e.getMessage());
		            }
		        }

			public void incluir(Imovel i) throws ImobiliariaException {
				try {
		             stIncluir.setString(1, i.getRegImovel());
		             stIncluir.(2, i.getCpfprop());
		             stIncluir.setString(3, i.getEndereco());
		             stIncluir.setString(4, i.getValor());
		             stIncluir.executeUpdate();
		               } catch(SQLException e) {
		                   throw new ImobiliariaException("Erro ao incluir novo imovel: " + e.getMessage());
		               }
		               
		              System.out.println(i);
			} 

			public Imovel[] consultaEndereco(String end) throws ImobiliariaException {
						
					Vector v = new Vector();
				try {
		             stConsultaEndereco.setString(1, end);
		             ResultSet rs = stConsultaEndereco.executeQuery();
		             	while(rs.next()) {
		             		Imovel i = new Imovel();
		                      i.setRegImovel(rs.getString("regimovel"));
		                      i.setEndereco(rs.getString("endereco"));
		                      i.setTipo(rs.getString("tipo"));
		                      v.add(i);
		                     }
				} catch(SQLException e) {
					throw new ImobiliariaException("Erro ao executar consulta de proprietario por nome: " + e.getMessage());
				}
			
				return (Proprietario[]) v.toArray(new Proprietario[0]);
			}
			
			
			public Proprietario consultaDataNasc(String dataNasc) throws ImobiliariaException {
				try {
		             stConsultaDataNasc.setString(1, dataNasc);
		             ResultSet rs = stConsultaDataNasc.executeQuery();
		             	while(rs.next()) {

		             		Proprietario p = new Proprietario();
		                      p.setNome(rs.getString("nome"));
		                      p.setDataNasc(rs.getString("datanasc"));
		                      p.setEndereco(rs.getString("endereco"));
		                      p.setCpf(rs.getString("cpfprop"));
		                      p.setTelefone(rs.getString("telefone"));
		                      p.setRg(rs.getString("rg"));
		                      p.setEmail(rs.getString("email"));
		                      p.setEstadoCivil(rs.getString("estadocivil"));
		                      return p;
		                        }
				} catch(SQLException e) {
					throw new ImobiliariaException("Erro ao executar consulta de proprietario por data de nascimento: " + e.getMessage());
				}
			
				return null;
			} 
			
			
			public Proprietario consultaPorCpf(String nome) throws ImobiliariaException {
				try {
		             stConsultaCpf.setString(1, nome);
		             ResultSet rs = stConsultaCpf.executeQuery();
		             	while(rs.next()) {
		             		Proprietario p = new Proprietario();
		                      p.setNome(rs.getString("nome"));
		                      p.setDataNasc(rs.getString("datanasc"));
		                      p.setEndereco(rs.getString("endereco"));
		                      p.setCpf(rs.getString("cpfprop"));
		                      p.setTelefone(rs.getString("telefone"));
		                      p.setRg(rs.getString("rg"));
		                      p.setEmail(rs.getString("email"));
		                      p.setEstadoCivil(rs.getString("estadocivil"));
		                      return p;
		                    }
				} catch(SQLException e) {
					throw new ImobiliariaException("Erro ao executar consulta de proprietario por CPF: " + e.getMessage());
				}
			
				return null;
			}
			

			public Proprietario[] listaNome() throws ImobiliariaException {
				Vector<Proprietario> result = new Vector<Proprietario>();
				try {
		             ResultSet rs = stListaNome.executeQuery();
		             	while(rs.next()) {
		             		Proprietario p = new Proprietario();
		                      p.setNome(rs.getString("nome"));
		                      p.setDataNasc(rs.getString("datanasc"));
		                      p.setEndereco(rs.getString("endereco"));
		                      p.setCpf(rs.getString("cpfcliente"));
		                      p.setTelefone(rs.getString("telefone"));
		                      p.setRg(rs.getString("rg"));
		                      p.setEmail(rs.getString("email"));
		                      p.setEstadoCivil(rs.getString("estadocivil"));
		                      result.add(p);
		                    }
				} catch(SQLException e) {
					throw new ImobiliariaException("Erro ao consultar proprietario cadastrados: " + e.getMessage());
				}
				
				return result.toArray(new Proprietario[0]);
			}
			
			public void alterar() throws ImobiliariaException {
				try {
					stAlterar.setString(1, );
					stAlterar.setString(2, p.getDataNasc());
					stAlterar.setString(3, p.getEndereco());
					stAlterar.setString(4, p.getCpf());
					stAlterar.setString(5, p.getTelefone());
					stAlterar.setString(6, p.getRg());
					stAlterar.setString(7, p.getEmail());
					stAlterar.setString(8, p.getEstadoCivil());
		            stAlterar.executeUpdate();
		            
				} catch(SQLException e) {
					throw new ImobiliariaException("Erro ao alterar dados do imovel: " + e.getMessage());
				} 		
			}



			}

	}


}
